#! usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "HuiZhang"


from selenium import webdriver
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.common.exceptions import WebDriverException, NoSuchElementException
import os
import time
from .server_tools import reset_database
from selenium.webdriver.common.keys import Keys
from .server_tools import create_session_on_server
from .management.commands.create_session import create_pre_authenticated_session
from django.conf import settings

MAX_WAIT = 10

def wait(fn):
	def modified_fn(*args, **kwargs):
		start_time = time.time()
		while True:
			try:
				return fn(*args, **kwargs)
			except  (AssertionError, WebDriverException) as e:
				if time.time() - start_time > MAX_WAIT:
					raise e
				time.sleep(0.5)
	return modified_fn

class FunctionalTest(StaticLiveServerTestCase):

	def setUp(self):
		# 打开Chrome浏览器
		self.browser = webdriver.Chrome()
		self.staging_server = os.environ.get('STAGING_SERVER')
		if self.staging_server:
			self.live_server_url = 'http://' + self.staging_server
			reset_database(self.staging_server)

	def tearDown(self):
		# 关闭浏览器
		self.browser.quit()

	# def wait(fn):
	# 	def modified_fn(*args, **kwargs):
	# 		start_time = time.time()
	# 		while True:
	# 			try:
	# 				return fn(*args, **kwargs)
	# 			except  (AssertionError, WebDriverException) as e:
	# 				if time.time() - start_time > MAX_WAIT:
	# 					raise e
	# 				time.sleep(0.5)
	# 	return modified_fn

	@wait
	def check_for_row_in_list_table(self, row_text):
		table = self.browser.find_element_by_id('id_list_table')
		rows = self.browser.find_elements_by_tag_name('tr')
		self.assertIn(row_text, [row.text for row in rows])

	@wait
	def wait_to_be_logged_in(self, email):
		self.browser.find_element_by_link_text('Log out')
		navbar = self.browser.find_element_by_css_selector('.navbar')
		self.assertIn(email, navbar.text)

	@wait
	def wait_to_be_logged_out(self, email):
		self.browser.find_element_by_name('email')
		navbar = self.browser.find_element_by_css_selector('.navbar')
		self.assertNotIn(email, navbar.text)

	@wait
	def wait_for(self, fn):
		return fn

	def get_item_input_box(self):
		return self.browser.find_element_by_id('id_text')
	
	def add_list_item(self, item_text):
		num_rows = len(self.browser.find_elements_by_css_selector('#id_list_table tr'))
		print(f'add {item_text}')
		self.get_item_input_box().send_keys(item_text)
		self.get_item_input_box().send_keys(Keys.ENTER)
		item_number = num_rows + 1
		self.check_for_row_in_list_table(f'{item_number}: {item_text}')

	def create_pre_authenticated_session(self, email):
		if self.staging_server:
			session_key = create_session_on_server(self.staging_server, email)
		else:
			session_key = create_pre_authenticated_session(email)

        # 为了设定cookie，要先访问网站
        # 404页面是加载最快的
		self.browser.get(self.live_server_url + "/404_no_such_url/")
		self.browser.add_cookie(dict(
			name=settings.SESSION_COOKIE_NAME,
			value=session_key,
			path='/'
		))








